import { getBaseUrl } from '@/services/urlconst';
import {
  getCompanyID,
  getPosIDs,
  getUserDepID,
  getUserID,
  requestFunComponFiex,
} from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();

/**
 * 流程节点
 * @param taskNodesCallBack
 */
const getTaskNodes = (
  taskNodesCallBack: (res: any) => void,
  controller: string,
) => {
  const taskNodes = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/${controller}/getTaskNodes/${
        params?.processID
      }/${params?.businessID}/${getUserDepID()}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: taskNodesCallBack,
    },
  );

  /**
   * 返回流程节点
   */
  const data = useMemo(() => {
    return taskNodes?.data?.obj;
  }, [taskNodes?.data]);

  return {
    taskNodesRun: taskNodes?.run,
    taskNodesLoading: taskNodes?.loading,
    taskNodesData: data,
  };
};

/**
 * 流程图
 * @param taskImgCallBack
 */
const getTaskImg = (
  taskImgCallBack: (res: any) => void,
  controller: string,
) => {
  const taskImg = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/${controller}/getProcdefImage/${params?.processID}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: taskImgCallBack,
    },
  );

  const data = useMemo(() => {
    return taskImg?.data;
  }, [taskImg?.data]);

  return {
    taskImgRun: taskImg?.run,
    taskImgLoading: taskImg?.loading,
    taskImgData: data,
  };
};

/**
 * 部门人员获取
 * loadUserByPosCallBack 回调
 */
const getUserListByOrgSrv = (getUserListByOrgCallBack: (res: any) => void) => {
  const getUserListByOrg = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/commonController/getUserListByOrg`;
      let bodyParams = {
        userPositionID: params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: getUserListByOrgCallBack,
    },
  );

  const data = useMemo(() => {
    return getUserListByOrg?.data?.obj;
  }, [getUserListByOrg?.data]);

  return {
    loadUserRun: getUserListByOrg?.run,
    loadUserLoading: getUserListByOrg?.loading,
    loadUserData: data,
  };
};

/**
 * 发起流程
 * startProcessCallBack 回调
 */
const startProcessService = (
  startProcessCallBack: (res: any) => void,
  controller: string,
) => {
  const startProcess = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/${controller}/startProcess/${getUserDepID()}`;
      let bodyParams = {
        ...params,
        ownCompanyID: getCompanyID(),
        ownDepartmentID: getUserDepID(),
        ownPositionID: getPosIDs(),
        ownerID: getUserID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: startProcessCallBack,
    },
  );

  return {
    startProcessRun: startProcess?.run,
    startProcessLoading: startProcess?.loading,
  };
};

/**
 * 查看流程图
 * @param getProcdefImageCallback
 * @returns
 */
const getProcdefImageService = (controller: string) => {
  const getProcdefImage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/${controller}/getProcdefImage/${params?.procDefID}`;

      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: () => {},
    },
  );

  const data = useMemo(() => {
    return getProcdefImage?.data;
  }, [getProcdefImage?.data]);

  return {
    getProcdefImageRun: getProcdefImage?.run,
    getProcdefImageLoading: getProcdefImage?.loading,
    getProcdefImageData: data,
  };
};

export {
  getTaskNodes,
  getTaskImg,
  getUserListByOrgSrv,
  startProcessService,
  getProcdefImageService,
};
